package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/1 20:27
 * @description：https://leetcode-cn.com/problems/avoid-flood-in-the-city/
 */
public class 避免洪水泛滥 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.avoidFlood(new int[]{0,1,1})));
    }

    class Solution {
        public int[] avoidFlood(int[] rains) {
            int len = rains.length;
            int[] ans = new int[len];
            Arrays.fill(ans, 1);
            HashMap<Integer, Integer> map = new HashMap<>();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < rains.length; i++) {
                if (rains[i] == 0) {
                    list.add(i);
                } else {
                    if (map.get(rains[i]) != null) {
                        if (list.isEmpty()) return new int[0];
                        Integer pre = map.get(rains[i]);
                        int index = 0;
                        for (; index < list.size(); index++) {
                            if (list.get(index) > pre) break;
                        }
                        if (index >= list.size()) return new int[0];
                        Integer first = list.get(index);
                        if (first > i || first < pre) return new int[0];
                        list.remove(index);
                        ans[first] = rains[i];
                    }
                    map.put(rains[i], i);
                    ans[i] = -1;
                }
            }
            System.out.println(Arrays.toString(ans));
            System.out.println(list);
            System.out.println(Collections.unmodifiableMap(map));
            return ans;
        }
    }
}
